<template>
  <div class="aside-center" id="asidecenter">

    <div class="isscroll ">
      <el-scrollbar style="height:100%;">
        <div type="text" class="search-title" v-if="!searchState.isInput" @click="handleIsinputClick">
          <img src="@/assets/images/secarh.png"/> 搜索全部稿件
        </div>

        <div class="search-result" v-else>
          <svg class="icon" aria-hidden="true" @click="handleClearValue">
            <use xlink:href="#icon-close"></use>
          </svg>
          <input type="text" class="search" v-on:blur="blurClear()" ref='inputFocus' @click="handleIsinputClick"
                 v-model="searchKeyWords" @keyup.enter="enterSearch()"/>
        </div>

        <p class="select-num" v-if="searchState.showResult">
          含有"<span>{{searchState.keyWords}}</span>"的结果共{{searchState.searchArticleTotal}}条</p>
        <div class="new-folder" @click="handleAddArticleClick" v-if="folderId!=-100">
          <i></i>新增文章
        </div>
        <ul class="list-box">
          <li v-if="articleList.length>0" v-for="(item,index) in articleList" :key="index"
              @click="handleSelectClick(index,item.articleId)"
              :class="{select:item.articleId == articleDetail.articleId}">
            <a>
              <h3>
                <span>{{item.title}}</span>
              </h3>
              <p>{{item.origContent}}</p>

              <div class="toollist clearfix">
                <span class="time">{{item._updateTimeFmt}}</span>
                <span class="comefrom">{{item.comefrom}}</span>
                <span class="comefrom-folder" v-if="searchKeyWords">
                 <svg class="icon icon-folder" aria-hidden="true">
                   <use xlink:href="#icon-folder"></use>
                 </svg>
                 {{item.folderName}}
             </span>
                <svg class="icon icon-trash" aria-hidden="true" @click="deletArticle(index,item.articleId)">
                  <use xlink:href="#icon-trash"></use>
                </svg>
              </div>
            </a>
          </li>
        </ul>
        <div v-if="isloadMore" class="cancel-btn" @click="loadmore">加载更多</div>
        <div v-if="!isloadMore" class="cancel-btn no-more">没有更多了</div>
      </el-scrollbar>

    </div>

  </div>

</template>

<script>
  import {mapState, mapActions} from 'vuex';
  import util from '@/lib/util';

  export default {
    name: 'AsideCenter',
    data() {
      return {
        pageNo: 1,
        searchPageNo: 1,
        searchKeyWords: '' //搜索的关键词
      };
    },
    computed: {
      ...mapState([
        'articleList',
        'folderId',
        'isloadMore',
        'articleDetail',
        'searchState' //搜索相关状态数据
      ]),
    },
    watch: {
      //监控全局变量folderid变化加载对应目录中的文章列表
      folderId(curVal, oldVal) {

        this._reset();
      }

    },
    methods: {
      ...mapActions([
        'fetchArticleList',
        'fetchFolderList', //新增和删除文章之后刷新文件夹列表
        'clearArticleList',
        'fetchSearchList',
        'deleteArticle',
        'changeSearchState',
        'activeArticle',
        'newArticle'
      ]),
      //状态重置
      _reset() {

        this.searchKeyWords = '';
        this.searchPageNo = 1;
        this.pageNo = 1;
        this.changeSearchState({
          isInput: false,
          showResult: false,
          keyWords: ''
        });
        //加载第一页此文件夹下的文章列表，加载方法会在加载的第一页的数据时候清空文章列表，并取第一篇文章
        this.fecthFoldArticleList();
      },
      //删除文章
      deletArticle(index, articleId) {
        let that = this;
        let param = {
          'id': articleId
        };
        util.ajax.post('/article/del', {
          ...param
        }).then((res) => {
          if (res.code == 1) {
            this.deleteArticle(articleId);
            //选中下一篇文章
            if (that.articleList.length > 0) {
              that.handleSelectClick(0, that.articleList[0].articleId);
            }
          }
        });
      },
      //处理文章选中事件
      handleSelectClick(index, articleId) {

        //激活新选择的文章,会加载对应的文章详情
        if (articleId != this.articleDetail.articleId) {
          this.activeArticle(articleId);
        }
      },
      //新增文章
      handleAddArticleClick() {
        if (this.folderId == -100) {
          return;
        }
        let that = this;
        //点击新增文章，恢复到未搜索状态
        this.searchKeyWords = '';
        this.searchPageNo = 1;
        this.changeSearchState({
          isInput: false,
          showResult: false,
          keyWords: ''
        });

        this.newArticle({
          'folderId': this.folderId
        }).then((res) => {
          that.pageNo = 1;
          this.fecthFoldArticleList(function () {
            //刷新文章列表之后设置新增的这篇文章为激活状态
            that.activeArticle(res.data.articleId);
          });
        });

      },
      handleIsinputClick() {
        this.changeSearchState({
          isInput: true
        });
        this.$nextTick(() => {
          this.$refs.inputFocus.focus();
        });
      },
      //搜索文章
      enterSearch() {
        this.changeSearchState({
          showResult: true,
          keyWords: this.searchKeyWords
        });

        //如果当前关键词为空，则加载当前目录中的文章列表
        if (!this.searchState.keyWords) {
          this.changeSearchState({
            isInput: false,
            showResult: false
          });
          //获取第一页目录文章列表
          this.pageNo = 1;
          this.fecthFoldArticleList();
          return;
        }
        let paramsValue = {
          'pageNo': this.searchPageNo,
          'searchKey': this.searchState.keyWords
        };
        this.fetchSearchList(paramsValue);
      },

      blurClear() {
        //如果搜索框中有关键字则不清空
        if (!this.searchKeyWords) {
          this.handleClearValue();
        }
      },
      //清空搜索文本框
      handleClearValue() {
        this._reset()
      },
      loadmore() {
        //加载更多来自search
        if (this.searchState.keyWords) {
          this.searchPageNo++;
          this.enterSearch();
          return;
        }
        //如果不是搜索状态则获取当前目录下的文章列表
        this.fecthFoldArticleList();
      },
      //加载目录中的文章
      fecthFoldArticleList(callback) {

        let paramsValue = {
          'pageNo': this.pageNo++,
          'folderId': this.folderId
        };
        let that = this;
        this.fetchArticleList(paramsValue).then((res) => {

          if (typeof callback === "function") {
            callback(res);
          }
        });
      }
    }
  }
</script>

<style lang="scss" scoped>
  @import "@/assets/css/style.scss";

  .aside-center {
    position: absolute;
    left: 221px;
    top: 83px;
    bottom: 0px;
    width: 300px;
    overflow: hidden;
    border-right: 1px solid $listborderColor;
    .isscroll {
      margin-right: -17px;
      /*width:100%;*/
      height: 100%;
      flex: 1;
      overflow-y: auto;
      overflow-x: hidden;
    }
    .search-title,
    .search {
      display: block;
      margin: 15px;
      padding: 0px 20px;
      line-height: 38px;
      font-size: $fs14;
      background: #f8f8f8;
      color: $listTitleColor;
      border-radius: 5px;
      border: 1px solid $listborderColor;
      cursor: pointer;
    }
    .search-title {
      width: 177px;
    }
    .search {
      margin: 0px;
      width: 228px;
    }
    .search-result {
      position: relative;
      margin: 15px;
      display: inline-block;
      .icon {
        position: absolute;
        top: 50%;
        right: 20px;
        margin: -10px;
        width: 20px;
        height: 20px;
        color: $listTitleColor;
      }
    }
    .select-num {
      padding: 20px;
      color: $listTitleColor;
      font-size: $fs14;
      height: 20px;
      span {
        color: $borderColor;
      }
    }
    .search-title {
      padding-left: 71px;
      color: $vfConcentColor;
      img {
        margin-right: 5px;
      }
    }
    .new-folder {
      margin: 20px auto;
      width: 160px;
      line-height: 40px;
      text-align: center;
      border-radius: 20px;
      border: 1px solid $listborderColor;
      color: $listTitleColor;
      cursor: pointer;
      /*// 十字icon*/
      i {
        position: relative;
        width: 8px;
        height: 8px;
        margin-right: 6px;
        &:before {
          content: '';
          display: block;
          position: absolute;
          top: 50%;
          left: 50%;
          transform: translate(-50%, -50%);
          width: 8px;
          height: 2px;
          background: $listTitleColor;
        }
        &:after {
          content: '';
          display: block;
          position: absolute;
          top: 50%;
          left: 50%;
          transform: translate(-50%, -50%);
          width: 2px;
          height: 8px;
          background: $listTitleColor;
        }
      }
    }
    .list-box {
      width: 300px;
      li {
        margin-right: 4px;
        padding-left: 10px;
        border-left: 3px solid $colorWhite;
        cursor: pointer;
        &:hover {
          background: $listBgColor;
          border-color: $listBgColor;
          .icon-trash {
            display: block !important;
          }
        }
        a {
          margin-right: 11px;
          display: block;
          padding: 10px 0px;
          border-bottom: 1px solid $borderbottomColor;
          h3,
          p {
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
          }
          h3 {
            margin-bottom: 4px;
            font-size: $fs18;
            font-weight: normal;
            line-height: 20px;
            color: $listTitleColor;
            span {
              float: left;
              margin-right: 16px;
              width: 240px;
              overflow: hidden;
              text-overflow: ellipsis;
              white-space: nowrap;
            }
            img {
              display: none;
              float: right;
              margin-right: 6px;
            }
          }
          p {
            margin-bottom: 10px;
            font-size: $fs14;
            line-height: 20px;
            color: $vfConcentColor;
          }
          .toollist {
            font-size: $fs14;
            line-height: 20px;
            color: $vfConcentColor;
            .icon {
              float: right;
              /*margin: 0px 10px 0px 0px;*/
              width: 18px;
              height: 18px;
            }
            .icon-trash {
              display: none;
            }
            .comefrom-folder {
              float: left;
              margin-right: 10px;
              width: 115px;
              overflow: hidden;
              text-overflow: ellipsis;
              white-space: nowrap;
            }
            .icon-folder {
              float: left;
              margin: 0px 5px 0px 0px;
            }
          }
          .time {
            display: inline-block;
            margin-right: 10px;
          }
          .time-right {
            float: right;
            margin: 0px;
            width: 120px;
          }
        }
      }
      .select {
        border-color: $borderColor !important;
        background: $listBgColor;
        .set-img {
          display: block !important;
        }
      }
    }

  }

  .cancel-btn {
    margin: 20px auto;
    width: 120px;
  }

  .no-more {
    border: 1px solid $borderbottomColor;
    color: $listborderColor;
  }
</style>
